import {Component, ViewChild} from '@angular/core';
import {Content, Events, IonicPage, NavController, NavParams} from 'ionic-angular';
import {InfoProvider} from "../../providers/InfoProvider";
import {HttpService} from "../../providers/HttpService";

/**
 * Generated class for the AnswerPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-answer',
  templateUrl: 'answer.html',
})
export class AnswerPage {

    @ViewChild(Content) content: Content;

    user_info: any;
    question: any;
    message_list: any[] = [];

    reply_data: any = {
        type: 1,
        content: null,
        question_id: null,
        qa_id: null,
        qa_code: null,
        qa_phone: null,
        qa_name: null,
        qa_gender: null
    };

    reply_label: string;

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private events: Events,
                private infoProvider: InfoProvider,
                private httpService: HttpService) {
        this.user_info = this.infoProvider.getUserInfo();
        this.question = this.navParams.get('question');
        console.log('question: ', this.question);

        this.reply_data.question_id = this.question.id;
        this.reply_data.qa_id = this.user_info.id;
        if (this.user_info.type > 1) {
            this.reply_data.type = 2;
            this.reply_data.qa_name = '管理员';
            this.reply_label = '回复' + this.question.user_phone;
        } else {
            this.reply_data.type = 1;
            this.reply_data.qa_code = this.user_info.code;
            this.reply_data.qa_phone = this.user_info.phone;
            this.reply_data.qa_name = this.user_info.name;
            this.reply_data.qa_gender = this.user_info.gender;
            this.reply_label = '继续提问';
        }

        if (this.infoProvider.isNative()) {
            this.events.subscribe('push:qa-reply', (data) => {
                console.log('push:qa-reply:', data);
                if (this.message_list.length > 0) {
                  this.message_list.push(data);
                } else {
                  this.getAnswerList(false).then(res => {
                    if (res.status == 0) {
                      this.message_list = res.data.rows;
                    }
                  });
                }
                this.scrollToBottom();
            });
        }
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad AnswerPage');
        this.getAnswerList().then(res => {
            if (res.status == 0) {
                this.message_list = res.data.rows;
                this.scrollToBottom();
            }
        });
    }

    getAvatarImage(gender: number, type: number = 1) {
        if (type > 1) {
            return 'assets/icon/admin.png';
        } else {
            return [
                'assets/icon/user.png',
                'assets/icon/man2.png',
                'assets/icon/woman2.png',
            ][gender];
        }
    }

    getAvatarStyle(type: number) {
        return {'right': type == 1, 'left': type == 2}
    }

    getContentStyle(type: number) {
        return {'right slide-right': type == 1, 'left slide-left': type == 2}
    }

    getDateStyle(type: number) {
        return {'right-date': type == 1, 'left-date': type == 2};
    }

    getAnswerList(loader: boolean = true): Promise<any> {
        return new Promise(resolve => {
            this.httpService.instance()
                .setUrl('/answer/getAnswer')
                .setQuery({
                    question_id: this.question.id
                })
                .showLoader(loader)
                .getRequest()
                .then(res => {
                    resolve(res);
                });
        });
    }

    onReplyMessage() {
        console.log('onReplyMessage: ', this.reply_data);
        if (!this.reply_data.content) {
            console.log('content missing');
            return;
        }

        this.httpService.instance()
            .setUrl('/answer/addAnswer')
            .setBody(this.reply_data)
            .postRequest()
            .then(res => {
                if (res.status == 0) {
                    this.message_list.push(res.data);
                    this.reply_data.content = null;
                    this.scrollToBottom();
                }
            });
    }

    scrollToBottom() {
        setTimeout(() => {
            this.content.scrollToBottom();
        }, 100);
    }

    onViewProfile(message) {
        console.log('viewProfile: ', message);
    }

    onRefresh(refresher) {
        this.getAnswerList(false).then(res => {
            if (res.status == 0) {
                this.message_list = res.data.rows;
                this.scrollToBottom();
            }
            refresher.complete();
        });
    }
}
